import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import ItemFrame from "../item/itemFrame";
import {getReward} from "../config/config";
import {globalConfig} from "../config/globalConfig";
import {model} from "../web/model/Model";

export enum InviteItemState {
  RECEIVED = -1,
  IRRECEIVABLE = 0,
  RECEIVABLE = 1,
}

@ccclass
export default class InviteItem extends cc.Component {
  private _state: InviteItemState = InviteItemState.RECEIVED

  @property(cc.Node)
  receivable: cc.Node = null
  @property(cc.Node)
  irreceivable: cc.Node = null
  @property(cc.Node)
  received: cc.Node = null
  @property(cc.Label)
  labelNum: cc.Label = null
  @property(cc.Label)
  labelComplete: cc.Label = null
  @property(cc.Node)
  itemFrame: cc.Node = null;

  private config: any;

  onLoad() {
    model.user.on('invite-change', () => {
      this.state = this.outValue
    }, this)
  }

  onDestroy() {
    model.user.targetOff(this)
  }

  set id(value: number) {
    const config = (globalConfig.invitereward[value - 1] as any)
    this.itemFrame.getComponent(ItemFrame).setRewardId(config.reward)
    this.labelNum.string = `邀请${config.target}位`
    this.labelComplete.string = `${model.user.msg.invite}/${config.target}`;
    this.config = config
    this.node.active = false
  }

  private outValue: number = 0;

  set state(value: number) {
    this.outValue = value
    if (value === 0 && model.user.msg.invite < this.config.target) {
      this.setState(InviteItemState.IRRECEIVABLE)
    } else if (value === -1) {
      this.setState(InviteItemState.RECEIVED)
    } else {
      this.setState(InviteItemState.RECEIVABLE)
    }
  }

  get state(): InviteItemState {
    return this._state;
  }

  private setState(value: InviteItemState) {
    this._state = value;
    this.receivable.active = false
    this.irreceivable.active = false
    this.received.active = false
    switch (value) {
      case InviteItemState.RECEIVED:
        this.received.active = true
        break
      case InviteItemState.RECEIVABLE:
        this.receivable.active = true
        break
      case InviteItemState.IRRECEIVABLE:
        this.irreceivable.active = true
        break
    }
    this.node.active = true
  }
}